﻿namespace BM.Tools.WebControls.MForm.Design
{
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Diagnostics.CodeAnalysis;
    using System.Web.UI.Design;
    using System.Web.UI.WebControls;

    using BM.Tools.WebControls.MForm.Controls;

    /// <summary>
    /// This is a <see cref="DesignerActionList" /> of the <see cref="Offspring"/> object. 
    /// It defines the actions that can be done on the <see cref="Offspring"/> object.
    /// </summary>
    public class OffspringActionList : DesignerActionList
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="OffspringActionList"/> class. 
        /// </summary>
        /// <param name="designer">
        /// The designer.
        /// </param>
        /// <param name="offspring">
        /// The offspring.
        /// </param>
        public OffspringActionList(ControlDesigner designer, Offspring offspring) : base(offspring)
        {
            Designer = designer;
            Offspring = offspring;
        }

        /// <summary>
        /// Gets Designer.
        /// </summary>
        public ControlDesigner Designer
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets or sets the control display name.
        /// </summary>        
        public string DisplayName
        {
            get
            {
                return Offspring.DisplayName;
            }

            set
            {
                SetProperty("DisplayName", value);
            }
        }

        /// <summary>
        /// Gets or sets the control name.
        /// </summary>        
        public string Name
        {
            get
            {
                return Offspring.Name;
            }

            set
            {
                SetProperty("Name", value);
            }
        }

        /// <summary>
        /// Gets or sets the control MaxOccurs.
        /// </summary>
        public int MaxOccurs
        {
            get
            {
                return Offspring.MaxOccurs;
            }

            set
            {
                SetProperty("MaxOccurs", value);
            }
        }

        /// <summary>
        /// Gets or sets the control MinOccurs.
        /// </summary>
        public int MinOccurs
        {
            get
            {
                return Offspring.MinOccurs;
            }

            set
            {
                SetProperty("MinOccurs", value);
            }
        }

        /// <summary>
        /// Gets the Offspring control.
        /// </summary>
        public Offspring Offspring
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets a value indicating whether the control is editable.
        /// </summary>
        protected virtual bool Editable
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Returns all the action items that can be performed on the control.
        /// </summary>
        /// <returns>
        /// the DesignerActionItemCollection.
        /// </returns>
        public override DesignerActionItemCollection GetSortedActionItems()
        {
            var items = new DesignerActionItemCollection();
            if (Editable)
            {
                items.Add(new DesignerActionHeaderItem("Information"));
                items.Add(new DesignerActionPropertyItem("Name", "Name", "Information", "the name of the element."));
                items.Add(new DesignerActionPropertyItem("DisplayName", "Display name", "Information", "the name of the element."));
                if (!(Offspring is Root))
                {
                    items.Add(
                            new DesignerActionPropertyItem(
                                    "MinOccurs", "minimal occurence number", "Information", string.Empty));
                    items.Add(
                            new DesignerActionPropertyItem(
                                    "MaxOccurs", "maximal occurence number", "Information", string.Empty));
                }

                items.Add(new DesignerActionHeaderItem("Appearance"));                
            }
            
            return items;
        }

        /// <summary>
        /// This method can be used to set the property of the control.
        /// </summary>
        /// <param name="target">
        /// The target.
        /// </param>
        /// <param name="propertyName">
        /// The property name.
        /// </param>
        /// <param name="value">
        /// The value.
        /// </param>
        protected void SetProperty(object target, string propertyName, object value)
        {
            PropertyDescriptor property = TypeDescriptor.GetProperties(target)[propertyName];
            property.SetValue(Offspring, value);
            Designer.OnComponentChanged(Component, new ComponentChangedEventArgs(Component, property, null, null));
        }

        /// <summary>
        /// This method can be used to set the property of the current offspring control.
        /// </summary>
        /// <param name="propertyName">
        /// The property name.
        /// </param>
        /// <param name="value">
        /// The value.
        /// </param>
        protected void SetProperty(string propertyName, object value)
        {
            SetProperty(Offspring, propertyName, value);
        }
    }
}